package com.google.code.ojar.sendtemplatemail.app.exception;

import org.apache.commons.mail.EmailException;

/**
 * Exception class that represent a send faliure
 * 
 * @author ottoabreu
 * 
 */
public class SendMailException extends Exception {
	/**
	 * SEND_ERROR_ADDRESS_ERROR=
	 * "Can't sent msg because cant create required addresses";
	 */
	public static final String SEND_ERROR_ADDRESS_ERROR = "Can't sent msg because cant create required addresses";
	/**
	 * SEND_ERROR_MESSAGING_ERROR=
	 * "Can't sent msg because was no posible to set a msg property";
	 */
	public static final String SEND_ERROR_MESSAGING_ERROR = "Can't sent msg because was no posible to set a msg property";
	/**
	 * SEND_MSG_ERROR
	 * ="Can't sent msg because cant set E-mail properties or can send msg ";
	 */
	public static final String SEND_MSG_ERROR = "Can't sent msg because cant set E-mail properties or can send msg ";

	private boolean connectionError;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public SendMailException() {
		super();
	
	}

	public SendMailException(String message, Throwable cause) {
		super(message, cause);
		this.getConnectionFaliure(cause);
	}

	public boolean isConnectionError() {
		return connectionError;
	}

	public SendMailException(String message) {
		super(message);
	
	}

	public SendMailException(Throwable cause) {
		super(cause);
		this.getConnectionFaliure(cause);
		
	}

	private void getConnectionFaliure(Throwable e) {

		// verifico si es causa de falla de conexion
		// el stack es EmailExcep--->MessagingExcep--->ConnectException
		if ((e instanceof EmailException)
				&& (e.getCause() != null
						&& e.getCause() instanceof javax.mail.MessagingException && (e
						.getCause().getCause() instanceof java.net.ConnectException))) {

			this.connectionError = true;

		}

	}

}
